// pages/shop/shop.js
// import utils from '../../../utils/utils';
import {
  categoryList,
  commodityList
} from '../../../common/api/shop.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: getApp().globalData.navBarHeight,
    statusBarHeight: getApp().globalData.statusBarHeight,
    bgHeight: 200,
    categoryData: [],
    activeIndex: 0,
    commodityArr: [],
    page: 1, // 页码
    goodsName: '', // 搜索名字
    bottomLoading: true,
    refresherTriggered: false,
    typeData: [
      {
        "name": "即将揭晓",
        "key": 'know'
      },
      {
        "name": "人气",
        "key": 'user'
      },
      {
        "name": "最新",
        "key": 'time'
      },
      // {
      //   "name": "金币",
      //   "key": 'price'
      // },
      // {
      //   "name": "金币",
      //   "key": 'idGold'
      // },
    ],
    activeType: 'time',
    tabHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideTabBar();
    // wx.navigateTo({
    //   url: "/packageA/calculationDetails/calculationDetails"
    // })
    this.setData({
      bgHeight: this.data.statusBarHeight > 20 ? (this.data.statusBarHeight - 20) * 2 + 200 : 200
    })
    this.getCategory();

// 解决底部遮挡问题
const res = wx.getSystemInfoSync()
const { screenHeight, safeArea: { bottom } } = res;
console.log('resHeight',res);
if (screenHeight && bottom){
  let safeBottom = screenHeight - bottom
  this.setData({
    tabHeight: 48 + safeBottom
  })
}

  },
  onShareAppMessage(e){
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../../images/shareImg.jpg",
       path: "/pages/index/index?id=" + user.id
    }
  },
  /**
   * 获取类目
   */
  getCategory: async function () {
    // const vm = this;
    // utils.get("category/list").then((res)=>{
    //   vm.setData({
    //     categoryData:res
    //   })
    // })
    let res = await categoryList({});
   
    this.setData({
      categoryData: res
    });
    setTimeout(() => {
      wx.showTabBar();
    });
    // 请求商品；
    this.getShop()
  },
  /**
   * 切换商品类目
   */
  cutCategory: function (e) {
    this.setData({
      activeIndex: e.currentTarget.dataset.index
    });
    this.bindreFresherrefresh()
  },
  cutType: function (e) {
    this.setData({
      activeType: this.data.typeData[e.currentTarget.dataset.index].key
    });
    this.bindreFresherrefresh()
  },
  /**
   * 搜索
   */
  getSeacrh: function(){
   this.bindreFresherrefresh()
  },
  /**
   * 获取商品列表
   */
  getShop: async function () {
    let data = {
      categoryId : this.data.categoryData[this.data.activeIndex].id,
      type: this.data.activeType,
      page: this.data.page,
      size: 10,
      goodsName: this.data.goodsName,
      isOnSale: 1,
      isBuy: 0,
      isGold: this.activeType == "idGold" ? 1 : 0,
    };
    let res = await commodityList(data);
    // console.log(res.list, '商品列表')
    // 测试分页
    // let arr2 = [];
    // for(let i=0;i<10;i++){
    //   arr2.push(...res.list)
    // };
    // let arr = arr2 || [];
    let arr = res.list || [];
      if(arr.length < data.size){
        this.setData({
          bottomLoading: false
        })
      }
    let a = [...this.data.commodityArr, ...arr];
    this.setData({
      commodityArr: a,
      refresherTriggered: false
    });
  },
  bindBottomLoading : function(){
    if(!this.data.bottomLoading){
      console.log('已经没有了')
      return
    }
    this.data.page+=1;
    this.getShop()
  },
  bindreFresherrefresh(){
    this.data.page = 1;
    this.setData({
      bottomLoading: true,
      commodityArr: []
    });
    this.getShop();
  },
  inputChange: function(e){
    this.data.goodsName = e.detail.value;
  },
  go: function (e) {
    wx.navigateTo({
      url: '../shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id + "&notCart=true",
    })
  },
 
})